package com.itzhigui.service.impl;

import com.itzhigui.dto.payInfoDto;
import com.itzhigui.entity.PayInfo;
import com.itzhigui.entity.User;
import com.itzhigui.mapper.PayInfoMapper;
import com.itzhigui.mapper.UserMapper;
import com.itzhigui.service.PayInfoService;
import com.itzhigui.utils.PageUtil;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: Zhangxy
 * @DateTime: 2023/11/17 10:39
 **/
@Service
@Transactional
public class PayInfoServiceImpl implements PayInfoService {
    @Resource
    private PayInfoMapper payInfoMapper;

    @Resource
    private UserMapper userMapper;
    @Override
    public ResponseUtil findAll() {
        Map<String, Object> map = new HashMap<>();
        List<PayInfo> payInfoList = payInfoMapper.findAll();
        List<payInfoDto> payInfoDtoList = new ArrayList<>();
        payInfoList.forEach(payInfo -> {
           //payInfo复制到payInfoDto
            payInfoDto payInfoDto = new payInfoDto();
            BeanUtils.copyProperties(payInfo,payInfoDto);
           // 设置用户信息
            Integer userId = payInfo.getUserId();
            User user = userMapper.findById(userId);
            user.setPassword(null);
            user.setSalt(null);
            payInfoDto.setUser(user);
            payInfoDtoList.add(payInfoDto);
        });

        return ResponseUtil.get(ResponseEnum.PAY_INFO_FINDALL_SUCCESS,payInfoDtoList);
    }

    @Override
    public ResponseUtil find(Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Integer startIndexNo = (page - 1) * pageSize;
        map.put("startIndexNo",startIndexNo);
        map.put("pageSize",pageSize);
        PageUtil<PayInfo> pageUtil = new PageUtil<>();
        pageUtil.setCurrentPageNo(page);
        pageUtil.setPageSize(pageSize);
        Integer totalCount = payInfoMapper.findCount(map);
        pageUtil.setTotalCount(totalCount);
        List<PayInfo> data = payInfoMapper.find(map);
        pageUtil.setData(data);
        return ResponseUtil.get(ResponseEnum.PAY_INFO_FINDALL_SUCCESS,pageUtil);
    }
}
